<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,
com.brother.model.*,com.ibatis.common.util.*,org.apache.commons.lang.*,org.json.JSONObject"   pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 
<%@ include file="international.jsp" %> 
<jsp:useBean id="userService" class="com.brother.service.UserService"></jsp:useBean>
<%

com.brother.model.Customer loginUser = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(loginUser ==null){
	return;
}
int start = Integer.parseInt(request.getParameter("start")==null?"0":request.getParameter("start"));
int limit = Integer.parseInt(request.getParameter("limit")==null?"20":request.getParameter("limit"));
String action = request.getParameter("action");
if(action != null && action.equalsIgnoreCase("delete")){
	String[] userids = request.getParameterValues("userid");
	for(String userid : userids){
		if(!userid.equals(loginUser.getUserid()))
			userService.deleteUser(loginUser, Integer.parseInt(userid));
	}
	response.sendRedirect("userList.jsp");
}

String userName = request.getParameter("userName");
if(userName != null) userName = userName.trim();
if(userName != null && userName.startsWith("Select")){
	userName = null;
}

List<Customer> users = userService.getChildUsers(loginUser.getUserid(), userName);
System.out.println(userName);
if(userName == null || userName.isEmpty() || loginUser.getName().startsWith(userName))
users.add(0, userService.GetUserByUserId(loginUser.getUserid()));

PaginatedArrayList pageData = new PaginatedArrayList(users, limit);
pageData.gotoPage(start/limit);

Iterator<Customer> datas = pageData.iterator();
int totalSize = users.size();
int totalPages = (totalSize % pageData.getPageSize() ==0) ?  (totalSize / pageData.getPageSize()): (totalSize / pageData.getPageSize()+ 1);

%>
{"totalCount":"<%=totalSize %>","models":[
<%
int count = 0;

while(datas.hasNext()){
	Customer user = datas.next();	 
	Customer superior = userService.getSuperiorUser(user.getUserid()+""); 
	user.setSupervisorName(superior != null ? superior.getName():"");
	count++;
	JSONObject json = new JSONObject(user);
	out.print(json.toString());
%>
<%if(datas.hasNext()){ %>,<%} %><%} %>]}					  
					  
<%
com.brother.database.DataBase.closeThreadConnection();
%>